//Classical Inheritance in JavaScript
//from http://javascript.crockford.com/inheritance.html
Function.prototype.method = function(name, func) {
	this.prototype[name] = func;
	return this;
};
Function.method('inherits', function(parent) {
	var d = {}, p = (this.prototype = new parent());
	this.method('uber', function uber(name) {
		if(!( name in d)) {
			d[name] = 0;
		}
		var f, r, t = d[name], v = parent.prototype;
		if(t) {
			while(t) {
				v = v.constructor.prototype;
				t -= 1;
			}
			f = v[name];
		} else {
			f = p[name];
			if(f == this[name]) {
				f = v[name];
			}
		}
		d[name] += 1;
		r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
		d[name] -= 1;
		return r;
	});
	return this;
});
Function.method('swiss', function (parent) {
    for (var i = 1; i < arguments.length; i += 1) {
        var name = arguments[i];
        this.prototype[name] = parent.prototype[name];
    }
    return this;
});
//begin of the jump
var jump = {};


//res is where and how we maintain the res of the application;
jump.res = function() {
    this.drawable=new jump.res.reslist();
    this.layout={};
    this.layout.fragment=new jump.res.layoutreslist();
    this.layout.activity=new jump.res.layoutreslist();
    this.layout.item=new jump.res.layoutreslist();
    this.layout.widget=new jump.res.layoutreslist();
    this.strings=new jump.res.reslist();
    this.raw=new jump.res.reslist();
};
var __jump_afterload__;
var __jump_afterload_t__;
function __jump_loadres__()
{
    var t=__jump_afterload_t__;
    if(
        t.drawable.loaded()&&
        t.layout.fragment.loaded()&&
        t.layout.activity.loaded()&&
        t.layout.item.loaded()&&
        t.layout.widget.loaded()&&
        t.strings.loaded()&&
        t.raw.loaded())
        {
            __jump_afterload__();
        }
        else{
        setTimeout('__jump_loadres__()',1000);}
}

jump.res.method('load',function(f){
    __jump_afterload__=f;
    __jump_afterload_t__=this;
    this.drawable.load();
    this.layout.fragment.load();
    this.layout.activity.load();
    this.layout.item.load();
    this.layout.widget.load();
    this.strings.load();
    this.raw.load();
    
    __jump_loadres__();
});
jump.res.item=function(url,name)
{
    this.name=name;
    this.url=url;
}
jump.res.item.prototype.load=function()
{
    var t=this;
    $.ajax({url:this.url,complete:function(data,status){t.postLoad(data.response);}});
}
jump.res.item.prototype.postLoad=function(data)
{
    this.loaded=true;
}

jump.res.item.layoutitem=function(url,name)
{
    this.name=name;
    this.url=url;
}
jump.res.item.layoutitem.inherits(jump.res.item);
jump.res.item.layoutitem.prototype.postLoad=function(data)
{
    this.uber('postLoad',data);
	this.layout=data;
}


//reslist is the universal list of res 
jump.res.reslist=function()
{
    this.list={};
}
jump.res.reslist.method('load',function()
{
    for(k in this.list)
    {
        this.list[k].load();
    }
});
jump.res.reslist.method('loaded',function()
{
    var p=true;
    for(k in this.list)
    {
        p=p&this.list[k].loaded;
    }
    return p;
});
jump.res.reslist.prototype.setCollectionXML=function(url)
{
    
}
jump.res.reslist.prototype.add=function(url,name)
{
    return this.list[name]=new jump.res.item(url,name);
}
jump.res.reslist.prototype.get=function(name)
{
    return this.list[name];
}
jump.res.layoutreslist=function(){ this.list={};};
jump.res.layoutreslist.inherits(jump.res.reslist);
jump.res.layoutreslist.prototype.add=function(url,name)
{
    return this.list[name]=new jump.res.item.layoutitem(url,name);
}

jump.viewGroup=function(){this.init();}
jump.viewGroup.method('init',function(){
    this.ids={};
    this.children=[];
});
jump.viewGroup.method('addId',function(v)
{
    if(v.Id)
    {
        this.ids[v.Id]=v;
    }
});
jump.viewGroup.method('addChild',function(v)
{
    if(v.id)
    {
        this.addId(v);
        this.children.push(v);
    }
});
jump.viewGroup.method('setLayout',function(layoutres){
	var inf=new jump.view.inflater();
	var v=inf.inflate(layoutres,this);
	this.setContent(v);
});
jump.viewGroup.method('findViewById',function(id)
{
	return this.ids[id];
});
jump.viewGroup.method('setContent',function(v)
{
    var vs=this.getHTMLSelector();
    for(var i=0;i<vs.length;i++)$(vs[i]).html(v);
});
jump.viewGroup.method('setFragment',function(f)
{
    f.setHTMLSelector(this.getHTMLSelector()[0]);
    f.onCreate();
});

//jump.view provide the views as a part of the UI
jump.view = {};



jump.view.View = function() {this.init();}
jump.view.View.inherits(jump.viewGroup);
jump.view.View.method('setHTMLSelector',function(id){this.mId=id;});
jump.view.View.method('getHTMLSelector',function(){return [this.mId];});

jump.view.inflater=function(Act)
{
    ;
}


jump.view.Widget=function(){this.init()};
jump.view.Widget.inherits(jump.viewGroup);
jump.view.Widget.method('setId',function(id){
    this.Id=id;
});
jump.view.Widget.method('setText',function(text){
    this.Text=text;
});
jump.view.Widget.method('getHTMLSelector',function(){
    return ["#"+this.Id];
});
jump.viewGroup.LinearLayout=function(){this.init()};
jump.viewGroup.LinearLayout.inherits(jump.view.Widget);
jump.viewGroup.LinearLayout.method('toHTML',function()
{
    var h="<div";
    if(this.Id)h+=" id='"+this.Id+"'";
    h+=">";
    for(var i=0;i<this.children.length;i++)
    {
        h+=this.children[i].toHTML();
    }
    return h+"</div>";
});


jump.view.Widget.Button=function(){this.init()};
jump.view.Widget.Button.inherits(jump.view.Widget);
jump.view.Widget.Button.method('toHTML',function()
{
    return "<input type='button' id='"+this.Id+"' value='"+this.Text+"'/>";
});

jump.view.Widget.TextView=function(){this.init()};
jump.view.Widget.TextView.inherits(jump.view.Widget);
jump.view.Widget.TextView.method('toHTML',function()
{
    return "<span id='"+this.Id+"'>"+this.Text+"<span/>";
});
//jump.activity provide a whole page of the UI
jump.activity = function() {this.init();}
jump.activity.inherits(jump.viewGroup);
jump.activity.method('getHTMLSelector',function(){
	return ["body"];
});

jump.activity.prototype.Start=function()
{
    this.onStart();
    this.LoadResources();
    this.onCreate();
    this.Show();
}
jump.activity.prototype.onStart = function() {
	;
}
jump.activity.prototype.LoadResources = function() {
	;
}
jump.activity.prototype.onCreate = function() {
	;
}
jump.activity.prototype.Show = function() {
	
}

//app is the main part;
jump.app = function() {
	this.res = new jump.res();
	this.activity = {};
}

jump.app.prototype.addActivity = function(name,A) {
    this.activity[name] = A;
	if(!this.initActivity) {
		this.initActivity = name;
	}
	return A;
}
jump.app.prototype.start = function() {
	if(this.initActivity) {
		this.activity[this.initActivity].Start();
	} else {
		alert("No Activity to start!");
	}

}
jump.app.method('loadResources',function(f){
    this.res.load(f);
});
jump.app.create = function(o) {
	var t = new jump.app();
	return t;
}


function __jump_inflater_2object__(res)
{
    var o={};
    var atrs=res.attributes;
    
    for(var i=0;i<atrs.length;i++)
    {
        var k=atrs[i].name;
        var v=atrs[i].value;
        if(k.indexOf('jump:')==0)
        {
            k=k.substr(5);
        }
        o[k]=v;
    }
    //o.id=$(res).attr("jump:id");
    o.Type=$(res).get(0).tagName;
    o.children=[];
    var c=$(res).children();
    for(var i=0;i<c.length;i++)
    {
        o.children.push(__jump_inflater_2object__(c[i]));
    }
    return o;
}
function __jump_inflater_2view__(obj,vg)
{
    var o;
    switch(obj.Type.toLowerCase())
    {
        case "linearlayout":
            o=new jump.viewGroup.LinearLayout();
            
            break;
        case "textview":
            o=new jump.view.Widget.TextView();
            break;
        case "button":
            o=new jump.view.Widget.Button();
            break;
        
    }
    o.setId(obj.id);
    o.setText(obj.text);
    for(var i=0;i<obj.children.length;i++)
    {
            o.children.push(__jump_inflater_2view__(obj.children[i],vg));
    }
    
    
    if(obj.id)
    {
        vg.addId(o);    
    }
    return o;
}
jump.view.inflater.prototype.inflate=function(res,vg)
{
    var obj=__jump_inflater_2object__($(res.layout)[1]);
    var o=__jump_inflater_2view__(obj,vg);
    return o.toHTML();
}
jump.fragment=function()
{
    this.init();
    this.mId=[];
}
jump.fragment.inherits(jump.viewGroup);
jump.fragment.method('setHTMLSelector',function(id){
    this.mId.push(id);
})
jump.fragment.method('getHTMLSelector',function(){
    return this.mId;
});


